import tkinter as tk
from tkinter import ttk
from VarBind import VarBind
class PortSetFrame(tk.Frame):
    def __init__(self,*argv,**kwards):
            super().__init__(*argv,**kwards)
            self._design()

    def _design(self):
        self.portargframe=PortArgFrame(self)
        self.portargframe.pack(fill=tk.X,anchor='n')
        self.sendare=tk.Text(self,height=4)
        self.sendare.pack(fill=tk.BOTH)
        # tk.Entry(self).pack(fill=tk.BOTH)
    def m_ClearSend(self):
        self.sendare.delete(0.0,'end')
    def m_BindVar(self,var:VarBind):
        self.portargframe.c_hexshow.configure(variable=var.vardict_bool["hexshow_bool"])
        self.portargframe.c_savetofile.configure(variable=var.vardict_bool["datatofile_bool"])
        self.portargframe.c_addtimepackshow.configure(variable=var.vardict_bool["timestap_bool"])
        self.portargframe.e_overtime.configure(textvariable=var.vardict_int["overlaytime_int"])
      
        self.portargframe.c_hexsend.configure(variable=var.vardict_bool["hexsend_bool"])
        self.portargframe.c_sendbytime.configure(variable=var.vardict_bool["stabletimesend_bool"])
        self.portargframe.e_numbertime.configure(textvariable=var.vardict_int["stabletime_int"])
        self.portargframe.c_returnenter.configure(variable=var.vardict_bool["enterreturn_bool"])

        self.portargframe.e_sendedhex.configure(textvariable=var.vardict_str["stri_str"])
        self.portargframe.cb_strisend.configure(variable=var.vardict_bool["stri_bool"])
    def m_GetStr(self):
        # x=self.sendare.get(1.0,'end')
        # if x=="233\n":
            
        #     print(x)
        # cc = "11\n22\n\nss\n"
        # zz=cc.strip()
        # print(zz.encode())
        # print(self.sendare.index('end'))
        return self.sendare.get(1.0,'end').strip()
class PortArgFrame(tk.Frame):
    def __init__(self,*argv,**kwards):
        super().__init__(*argv,**kwards)
        self._design()
    def _design(self):
        #set area
        self.f_setarea =tk.Frame(self)
        #recv show
        self.f_recv=tk.Frame(self.f_setarea)
        self.f_recv1=tk.Frame(self.f_recv)
        self.f_recv2=tk.Frame(self.f_recv)
        self.c_hexshow = tk.Checkbutton(self.f_recv1,text="HEX显示")
        self.b_savedata = tk.Button(self.f_recv1,text="保存数据")
        self.c_savetofile = tk.Checkbutton(self.f_recv1,text="接收数据到文件")
        self.c_addtimepackshow=tk.Checkbutton(self.f_recv2,text="加时间戳和分包显示")
        self.l_overtime = tk.Label(self.f_recv2,text="超时时间(ms):")
        self.e_overtime = tk.Entry(self.f_recv2,width=4)
        #send set
        self.f_send = tk.Frame(self.f_setarea)
        self.f_send1=tk.Frame(self.f_send)
        self.f_send2=tk.Frame(self.f_send)
        self.c_hexsend = tk.Checkbutton(self.f_send1,text="HEX发送")
        self.c_sendbytime = tk.Checkbutton(self.f_send1,text="定时发送:")
        self.e_numbertime = tk.Entry(self.f_send1,width=4)
        self.l_mstime = tk.Label(self.f_send1,text="ms/次")
        self.c_returnenter=tk.Checkbutton(self.f_send1,text="加回车换行")
        

        self.l_sendedhex=tk.Label(self.f_send2,text="已发送HEX:")
        self.e_sendedhex=tk.Entry(self.f_send2,text="FF AA CC")
        self.cb_strisend=tk.Checkbutton(self.f_send2,text="直接发送左侧HEX")
        self._layout()
    def _layout(self):
        self.f_setarea.pack(fill=tk.X,anchor='w')
        self.f_recv.pack(side='left',fill=tk.X)
        self.f_recv1.pack(fill=tk.X,anchor='w')
        self.f_recv2.pack(fill=tk.X,anchor='w')

        self.c_hexshow.pack(side='left',anchor='w')
        self.b_savedata.pack(side='left')
        self.c_savetofile.pack(side='left')
        self.c_addtimepackshow.pack(side='left')
        self.l_overtime.pack(side='left') 
        self.e_overtime.pack(side='left')

        self.f_send.pack(side='left',anchor='n',fill=tk.X)
        self.f_send1.pack(side='top',fill=tk.X)
        self.f_send2.pack(side='top',fill=tk.X)
        self.c_hexsend.pack(side='left')
        self.c_sendbytime.pack(side='left')
        self.e_numbertime.pack(side='left')
        self.l_mstime.pack(side='left')
        self.c_returnenter.pack(side='left')

        self.l_sendedhex.pack(side='left')
        self.e_sendedhex.pack(side='left')
        self.cb_strisend.pack(side='left')



if __name__=="__main__":
    win = tk.Tk()

    xe = PortSetFrame(win)
    xe.pack(fill=tk.X)
    tk.Button(win,command=xe.m_GetStr).pack()
    tk.Button(win,command=xe.m_ClearSend).pack()
    win.mainloop()